package com.example.renttest.main.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.renttest.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FoodAdapter extends BaseAdapter {

    private List<Map<String,Object>> list;
    public List<Map<String, Object>> car = new ArrayList<>();

    public FoodAdapter(List<Map<String,Object>> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view==null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_zhujirmian2, null);
            holder = new Holder();
            holder.imageView = view.findViewById(R.id.imageView);
            holder.textView = view.findViewById(R.id.textView);
            holder.textView2 = view.findViewById(R.id.textView2);
            holder.textView3 = view.findViewById(R.id.textView3);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();
        }
        String name = (String) list.get(i).get("name");
        holder.textView.setText(name);

        Double price = (Double) list.get(i).get("price");
        holder.textView2.setText(price+"");

        String description = (String) list.get(i).get("description");
        holder.textView3.setText(description);

        Picasso.with(viewGroup.getContext())
                .load("http://"+viewGroup.getContext().getString(R.string.ip)+"/foodserver/"+list.get(i).get("icon"))
                .into(holder.imageView);

        Button useBtn=(Button)view.findViewById(R.id.button2);
        useBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                car.add(list.get(i));
                Toast.makeText(viewGroup.getContext(), "已添加", Toast.LENGTH_SHORT).show();
            }
        });

        view.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(viewGroup.getContext(), "点赞", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    class Holder{
        ImageView imageView;
        TextView textView;
        TextView textView2;
        TextView textView3;
    }
}
